#!/bin/sh
basepath="/opt/metricas-turing"
time=`date +%s`

if [ ! $1 ]; then
	echo "`date` Parametro data nao fornecido, utilizando `date +%Y-%m-%d -d '1 day ago'`."
        data=`date +%Y-%m-%d -d '1 day ago'`;
else
        data=$1
fi

$basepath/import_turing_request_log.bsh $data		
while [[ "`gunzip -tq $basepath/a2-jubilo1-turing-request.log.$data;echo $?`" == "1" ]] || \
      [[ "`gunzip -tq $basepath/a2-jubilo2-turing-request.log.$data;echo $?`" == "1" ]] || \
      [[ "`gunzip -tq $basepath/a2-jubilo3-turing-request.log.$data;echo $?`" == "1" ]]
do
  echo "`date` Downloaded files are empty. Retrying in 60 seconds"
  sleep 60
  $basepath/import_turing_request_log.bsh $data
done

echo "===================================================" >$basepath/mail.letter-$data
echo "     Relatorio do Turing para o dia $data          " >>$basepath/mail.letter-$data
echo "===================================================" >>$basepath/mail.letter-$data
zcat $basepath/a2-jubilo?-turing-request.log.$data.gz | $basepath/stats.pl >>$basepath/mail.letter-$data 2>>/tmp/metricas-turing.log
time_elapsed=$(echo "`date +%s` - $time" | bc)
echo "===================================================" >>$basepath/mail.letter-$data
echo " Relatorio processado em $time_elapsed segundos.   " >>$basepath/mail.letter-$data

#cat $basepath/mail.letter-$data | mailx -s "[Turing] Relatorio para o dia $data" -r l-tec-log@uolinc.com asantos@uolinc.com dmanzato@uolinc.com raratame@uolinc.com l-adm-n1@uolinc.com azanarotti@uolinc.com nnovaes@uolinc.com
cat $basepath/mail.letter-$data | mailx -s "[Turing] Relatorio para o dia $data" l-tec-alta-audiencia@uolinc.com dmanzato@uolinc.com raratame@uolinc.com nnovaes@uolinc.com l-dev-pd-infra-secprod@uolinc.com l-dev-pd-infra-chat@uolinc.com
#cat $basepath/mail.letter-$data | mailx -s "[Turing] Relatorio para o dia $data" jdourado@uolinc.com

rm $basepath/*$data* -f
